package logic

import (
	"context"
	"errors"
	"video/models/mysql"
	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoAppLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoAppLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoAppLogic {
	return &VideoAppLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频上传
func (l *VideoAppLogic) VideoApp(in *videorpc.VideoAppRequest) (*videorpc.VideoAppResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("用户未注册,不允许发布视频")
	}
	video := &mysql.Video{

		VideoTitle:    in.VideoTitle,
		VideoCategory: in.VideoCategory,
		VideoLink:     in.VideoLink,
		UserId:        int(in.UserId),
	}
	err = video.VideoAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("发布失败")
	}
	return &videorpc.VideoAppResponse{
		VideoId: int64(video.ID),
	}, nil
}
